package com.luolc.syllabustest.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luolc.syllabustest.R;

import java.util.List;



/**
 * 对话框辅助类
 *
 * @Author wk
 * @Date 2015-9-6 12:19
 */
public class DialogUtils {

    /**
     * 获取一个dialog
     *
     * @param context
     * @return
     */
    public static AlertDialog.Builder getDialog(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        return builder;
    }

    /**
     * 等待的对话框
     *
     * @param context
     * @param message
     * @return
     */
    public static ProgressDialog getWaitDialog(Context context, String message) {
        ProgressDialog waitDialog = new ProgressDialog(context);
        waitDialog.setCanceledOnTouchOutside(false);
        waitDialog.setCancelable(true);
        if (!TextUtils.isEmpty(message)) {
            waitDialog.setMessage(message);
        }
        return waitDialog;
    }

    /**
     * 获取一个信息对话框，注意需要自己手动调用show方法显示
     *
     * @param context
     * @param message
     * @param onClickListener
     * @return
     */
    public static AlertDialog.Builder getMessageDialog(Context context, String message, DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(message);
        builder.setPositiveButton("确定", onClickListener);
        return builder;
    }

    public static AlertDialog.Builder getMessageDialog(Context context, String message) {
        return getMessageDialog(context, message, null);
    }


    public static AlertDialog getMenuDialog(Context context, List<String> titiles, View.OnClickListener onClickListener) {
        AlertDialog alertDialog = null;
        AlertDialog.Builder builder = getDialog(context);
        builder.setView(createView(context, titiles, onClickListener));
        alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(true);

        return alertDialog;
    }

    interface ItemClickListener {
        void onItemClick(View view, AlertDialog alertDialog);
    }

    private static LinearLayout createView(Context context, List<String> titles, View.OnClickListener onClickListener) {
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        for (String title : titles) {
            TextView textView = new TextView(context);
            textView.setBackgroundResource(R.drawable.shap_item_default);
            textView.setGravity(Gravity.CENTER);
            textView.setText(title);
            textView.setOnClickListener(onClickListener);
            textView.setTag(title);
            linearLayout.addView(textView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) context.getResources().getDimension(R.dimen.android_design_btn_size)));
            View view = LayoutInflater.from(context).inflate(R.layout.view_divider_horizontal, linearLayout, false);
            linearLayout.addView(view);
        }
        return linearLayout;
    }


    public static AlertDialog getConfirmDialog(Context context, String title, View view, DialogInterface.OnClickListener onOkClickListener, DialogInterface.OnClickListener onCancleClickListener) {
        AlertDialog alertDialog;

        AlertDialog.Builder builder = getDialog(context);
        builder.setTitle(title);
        builder.setView(view);
        builder.setPositiveButton("确定", onOkClickListener);
        builder.setNegativeButton("取消", onCancleClickListener);
        alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(true);
//        Window window = alertDialog.getWindow();
//        WindowManager.LayoutParams params = window.getAttributes();
//        params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
//        window.setAttributes(params);
        return alertDialog;
    }

    public static AlertDialog.Builder getConfirmDialog(Context context, String title, String
            message, DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setTitle(title);
        builder.setMessage(Html.fromHtml(message));
        builder.setPositiveButton("确定", onClickListener);
        builder.setNegativeButton("取消", null);
        return builder;
    }

    public static AlertDialog.Builder getConfirmDialog(Context context, String message, DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(Html.fromHtml(message));
        builder.setPositiveButton("确定", onClickListener);
        builder.setNegativeButton("取消", null);
        return builder;
    }

    public static AlertDialog.Builder getConfirmDialog(Context context, String message, DialogInterface.OnClickListener onOkClickListener, DialogInterface.OnClickListener onCancleClickListener) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(message);
        builder.setPositiveButton("确定", onOkClickListener);
        builder.setNegativeButton("取消", onCancleClickListener);
        return builder;
    }

    public static AlertDialog.Builder getSelectDialog(Context context, String title, String[] arrays, DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setItems(arrays, onClickListener);
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        }
        builder.setPositiveButton("取消", null);
        return builder;
    }

    public static AlertDialog.Builder getSelectDialog(Context context, String[] arrays, DialogInterface.OnClickListener onClickListener) {
        return getSelectDialog(context, "", arrays, onClickListener);
    }

    public static AlertDialog.Builder getSingleChoiceDialog(Context context, String title, String[] arrays, int selectIndex, DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setSingleChoiceItems(arrays, selectIndex, onClickListener);
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        }
        builder.setNegativeButton("取消", null);
        return builder;
    }

    public static AlertDialog.Builder getSingleChoiceDialog(Context context, String[] arrays, int selectIndex, DialogInterface.OnClickListener onClickListener) {
        return getSingleChoiceDialog(context, "", arrays, selectIndex, onClickListener);
    }


}
